/*
 * PaintBoard - Draw on board with your friends
 * Copyright (C) 2010
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02111-1301, USA.
 */

#define PURPLE_PLUGINS

#include <glib.h>
#include <gtkblist.h>
#include <gtkplugin.h>
#include <gtk/gtk.h>
#include <plugin.h>
#include <version.h>
#include <debug.h>
#include <stdio.h>
#include <stdlib.h>
#include <notify.h>
#include <prpl.h>
#include <request.h>
#include <signals.h>
#include <util.h>
#include <account.h>
#include <connection.h>
#include <glib-object.h>
#include <glib/gstdio.h>
#include <gmodule.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <locale.h>
#define N_(String) (String)
#ifndef _
#  define _(String) ((const char *)String)
#endif
#define ngettext(Singular, Plural, Number) ((Number == 1) ? ((const char *)Singular) : ((const char *)Plural))
#define dngettext(Domain, Singular, Plural, Number) ((Number == 1) ? ((const char *)Singular) : ((const char *)Plural))

#define PLUGIN_ID "paintboard"

// Copy the image
static GdkPixmap *pixmap = NULL;
enum brush { PENCIL, LINE, RECTANGLE, CIRICLE, ERASER, TEXT, CLEAN};
enum brush actuall_brush = PENCIL;
GtkWidget *label_brush;
GtkWidget *label_size;
GtkWidget *label_color;
gboolean first_point = TRUE;
gdouble temp_x;
gdouble temp_y;
gdouble brush_size = 8;
GdkGC *color_gc;
GdkGC *friend_color_gc;
GdkColor colors; 
GdkColor friend_colors;
GdkGCValues* color_values;
GdkFont *font; 
GtkWidget *text_entry;
GtkWidget *figure_filled;
gboolean figureFilled = TRUE;

PurpleBuddy* buddy;
PurplePlugin* plugin;
PurpleConversation* conv;
char* color = "black";
GtkWidget* my_widget;
gboolean isWindowOpen = FALSE;

// This function is responsible for processing the received data.
	
static void
receive_draw_brush(GtkWidget *widget, char* brush_type, char* color, int x1, int y1, int x2, int y2, char* text, char* filled)
{
        GdkRectangle update_rect;
	friend_color_gc = gdk_gc_new (widget->window);
	gboolean r_figureFilled = TRUE;

        if(strcoll(color, "black") == 0)
        {
			friend_colors.red = 0; friend_colors.green = 0; friend_colors.blue = 0; // black
        }
        if(strcoll(color, "white") == 0)
        {
			friend_colors.red = 0xFFFF; friend_colors.green = 0xFFFF; friend_colors.blue = 0xFFFF; // white
        }
        if(strcoll(color, "red") == 0)
        {
			friend_colors.red = 0xFFFF; friend_colors.green = 0; friend_colors.blue = 0; // red
        }
        if(strcoll(color, "blue") == 0)
        {
			friend_colors.red = 0; friend_colors.green = 0; friend_colors.blue = 0xFFFF; // blue
        }
        if(strcoll(color, "green") == 0)
        {
			friend_colors.red = 0; friend_colors.green = 0xFFFF; friend_colors.blue = 0; // green
        }
        if(strcoll(color, "yellow") == 0)
        {
			friend_colors.red = 0xFFFF; friend_colors.green = 0xFFFF; friend_colors.blue = 0; // yellow
        }
	if (filled) {
        	if(strcoll(filled, "TRUE") == 0)
        	{
				r_figureFilled = TRUE;
        	}
        	if(strcoll(filled, "FALSE") == 0)
        	{
				r_figureFilled = FALSE;
        	}
	}
        
        gdk_colormap_alloc_color (gdk_colormap_get_system(), &friend_colors, FALSE, TRUE);
        gdk_gc_set_foreground (friend_color_gc, &friend_colors);
        if(strcoll(brush_type, "pencil") == 0)
        {
                gdk_draw_arc  (pixmap, friend_color_gc, TRUE, x1, y1, x2, y2, 0, 64 *360);
                purple_debug_info(PLUGIN_ID, "DRAW A PENCIL IN PERSON WINDOW\n");
        }
        if(strcoll(brush_type, "eraser") == 0)
        {
                gdk_draw_arc  (pixmap, widget->style->white_gc, TRUE, x1, y1, x2, y2, 0, 64 *360);
                purple_debug_info(PLUGIN_ID, "DRAW A ERASER IN PERSON WINDOW\n");
        }
		if(strcoll(brush_type, "clean") == 0)
        {
                gdk_draw_rectangle  (pixmap, widget->style->white_gc, TRUE, 0, 0, 525, 445);
                purple_debug_info(PLUGIN_ID, "CLEAN BOARD IN PERSON WINDOW\n");
        }
        if(strcoll(brush_type, "text") == 0)
        {
                font=gdk_font_load("-adobe-helvetica-medium-r-normal--*-120-*-*-*-*-*-*");
                gdk_draw_string(pixmap,font, friend_color_gc, x1, y1, text);
                purple_debug_info(PLUGIN_ID, "DRAW A TEXT IN PERSON WINDOW\n");
        }
		if(strcoll(brush_type, "line") == 0)
        {
                gdk_draw_line  (pixmap, friend_color_gc, x1, y1, x2, y2);
                purple_debug_info(PLUGIN_ID, "DRAW A LINE IN PERSON WINDOW\n");
        }
        if(strcoll(brush_type, "circle") == 0)
        {
                gdk_draw_arc  (pixmap, friend_color_gc, r_figureFilled, x1, y1, x2, y2, 0, 64 *360);
                purple_debug_info(PLUGIN_ID, "DRAW A CIRCLE IN PERSON WINDOW\n");
        }
        if(strcoll(brush_type, "rectangle") == 0)
        {
                gdk_draw_rectangle  (pixmap, friend_color_gc, r_figureFilled, x1, y1, x2, y2);
                purple_debug_info(PLUGIN_ID, "DRAW A RECTANGLE IN PERSON WINDOW\n");
        }

        gtk_widget_draw (widget, &update_rect);
        gdk_draw_pixmap(widget->window, widget->style->fg_gc[GTK_WIDGET_STATE (widget)], pixmap, 0, 0, 0, 0, 525, 445);
        
}

// Sending the image.
static void
do_it_cb(const char *message)
{
        char *stripped;

        purple_markup_html_to_xhtml(message, NULL, &stripped);

        conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, buddy->account, buddy->name);
        if(conv) {
                if (buddy->account->gc->flags & PURPLE_CONNECTION_HTML)
                        purple_conv_im_send_with_flags(PURPLE_CONV_IM(conv), message, PURPLE_MESSAGE_NO_LOG | PURPLE_MESSAGE_INVISIBLE );
                else
                        purple_conv_im_send_with_flags(PURPLE_CONV_IM(conv), stripped, PURPLE_MESSAGE_NO_LOG  | PURPLE_MESSAGE_INVISIBLE  );
        }

        g_free(stripped);
}

// Function returning the minimum.
static gdouble 
min (gdouble x, gdouble y)
{
        if(x < y) return x;
        else return y;
}

// Function returning the maximum.
static gdouble 
max (gdouble x, gdouble y)
{
        if(x > y) return x;
        else return y;
}

// Utowrzenie podloza obrazka o odpowiednim rozmiarze 
// Have been setup background image of the appropriate size????
static gint
configure_event(GtkWidget *widget, GdkEventConfigure *event)
{
        if (pixmap)
                gdk_pixmap_unref(pixmap);

        pixmap = gdk_pixmap_new(widget->window, widget->allocation.width, widget->allocation.height, -1);
        gdk_draw_rectangle (pixmap, widget->style->white_gc, TRUE, 0, 0, widget->allocation.width, widget->allocation.height);
        my_widget = widget;
        
        return TRUE;
}

// Funkcja przerysowujaca obrazek z podkladu
// Function redrawing the image of primer..probably meant background?
static gint
expose_event(GtkWidget *widget, GdkEventExpose *event)
{
        gdk_draw_pixmap(widget->window, widget->style->fg_gc[GTK_WIDGET_STATE (widget)], pixmap, event->area.x, event->area.y, event->area.x, event->area.y, event->area.width, event->area.height);
        return FALSE;
}

// Draw
static void
draw_brush(GtkWidget *widget, gdouble x, gdouble y)
{
        GdkRectangle update_rect;
        
        if(color_gc == NULL)
			color_gc = widget->style->black_gc;
        
        if(actuall_brush == PENCIL)
        {
            update_rect.width = brush_size;
            update_rect.height = brush_size;
            update_rect.x = x - update_rect.width/2;
            update_rect.y = y - update_rect.height/2;
            gdk_draw_arc  (pixmap, color_gc, TRUE, update_rect.x, update_rect.y, update_rect.width, update_rect.height, 0, 64 *360);
			char* msg = g_strdup_printf(":%s:pencil:%s:%.0f,%.0f:%.0f,%.0f:%s:%s:", PLUGIN_ID, color, x - brush_size/2, y - brush_size/2, brush_size, brush_size, " ", " ");
            do_it_cb(msg);
            purple_debug_info(PLUGIN_ID, "INFO pencil at: %d, %d.\n", update_rect.x, update_rect.y);
        }
        if(actuall_brush == ERASER)
        {
            update_rect.width = 8;
            update_rect.height = 8;
            update_rect.x = x - update_rect.width/2;
            update_rect.y = y - update_rect.height/2;
            gdk_draw_arc  (pixmap, widget->style->white_gc, TRUE, update_rect.x, update_rect.y, update_rect.width, update_rect.height, 0, 64 *360);
			char* msg = g_strdup_printf(":%s:eraser:%s:%.0f,%.0f:%.0f,%.0f:%s:%s:", PLUGIN_ID, color, x - brush_size/2, y - brush_size/2, brush_size, brush_size, " ", " ");
            do_it_cb(msg);
            purple_debug_info(PLUGIN_ID, "INFO eraser at: %d, %d.\n", update_rect.x, update_rect.y);
        }
        if(actuall_brush == CLEAN)
        {
            update_rect.width = 525;
            update_rect.height = 445;
            update_rect.x = 0;
            update_rect.y = 0;
            gdk_draw_rectangle  (pixmap, widget->style->white_gc, TRUE, update_rect.x, update_rect.y, update_rect.width, update_rect.height);
			char* msg = g_strdup_printf(":%s:clean:%s:%d,%d:%d,%d:%s:%s:", PLUGIN_ID, color, 0, 0, 0, 0, " ", " ");
            do_it_cb(msg);
            purple_debug_info(PLUGIN_ID, "INFO clean all.\n");
        }
        if(actuall_brush == TEXT)
        {
                update_rect.width = 525;
                update_rect.height = 445;
                update_rect.x = 0;
                update_rect.y = 0;
                font=gdk_font_load("-adobe-helvetica-medium-r-normal--*-120-*-*-*-*-*-*");
				char* msg = g_strdup_printf(":%s:text:%s:%.0f,%.0f:%d,%d:%s:%s:", PLUGIN_ID, color, x, y, 0, 0, gtk_entry_get_text(GTK_ENTRY(text_entry)), " ");
				do_it_cb(msg);
                gdk_draw_string(pixmap,font, color_gc,x,y,gtk_entry_get_text(GTK_ENTRY(text_entry)));
        }
        if(actuall_brush == LINE || actuall_brush == CIRICLE || actuall_brush == RECTANGLE)
        {
                if(first_point == TRUE)
                {
                    temp_x = x;
                    temp_y = y;
                    first_point = FALSE;
                }
                else
                {
                    update_rect.width = max(temp_x, x) - min(temp_x, x);
                    update_rect.height = max(temp_y, y) - min(temp_y, y);
                    update_rect.x = min(temp_x, x);
                    update_rect.y = min(temp_y, y);
                    if(actuall_brush == LINE)
                    {
                                gdk_draw_line  (pixmap, color_gc, x, y, temp_x, temp_y);
                                char* msg = g_strdup_printf(":%s:line:%s:%.0f,%.0f:%.0f,%.0f:%s:%s:", PLUGIN_ID, color, x, y, temp_x, temp_y, " ", " ");
                                do_it_cb(msg);
                    }
                    if(actuall_brush == CIRICLE)
                    {
                                gdk_draw_arc  (pixmap, color_gc, figureFilled, min(temp_x, x), min(temp_y, y),  max(temp_x, x) - min(temp_x, x) - 1, max(temp_y, y) - min(temp_y, y) - 1, 0, 64 *360);
								char* msg = g_strdup_printf(":%s:circle:%s:%.0f,%.0f:%.0f,%.0f:%s:%s:", PLUGIN_ID, color, min(temp_x, x), min(temp_y, y), max(temp_x, x) - min(temp_x, x), max(temp_y, y) - min(temp_y, y), " ", (figureFilled)?"TRUE":"FALSE");
                                do_it_cb(msg);
                    }
                    if(actuall_brush == RECTANGLE)
                    {
                                gdk_draw_rectangle  (pixmap, color_gc, figureFilled, min(temp_x, x), min(temp_y, y),  max(temp_x, x) - min(temp_x, x) - 1, max(temp_y, y) - min(temp_y, y) - 1);
								char* msg = g_strdup_printf(":%s:rectangle:%s:%.0f,%.0f:%.0f,%.0f:%s:%s:", PLUGIN_ID, color, min(temp_x, x), min(temp_y, y), max(temp_x, x) - min(temp_x, x), max(temp_y, y) - min(temp_y, y), " ", (figureFilled)?"TRUE":"FALSE");
                                do_it_cb(msg);
                    }
                    first_point = TRUE;
                }
        }
        
        gtk_widget_draw (widget, &update_rect);
}

// Pressing the button. 
static gint button_press_event( GtkWidget *widget, GdkEventButton   *event )
{
        if (event->button == 1 && pixmap != NULL)
                draw_brush(widget, event->x, event->y);
        
        return TRUE;
}

// Ruch przy wcisnietym przycisku
// Move button while holding down the ----. (source of translation just ends. "button" might mean pencil point?)
static gint
motion_notify_event(GtkWidget *widget, GdkEventMotion *event)
{
        if(actuall_brush == ERASER || actuall_brush == PENCIL){
          
        int x, y;
        GdkModifierType state;
        
        if (event->is_hint) {
                gdk_window_get_pointer(event->window, &x, &y, &state);
        }
        else {
                x = event->x;
                y = event->y;
                state = event->state;
        }
        
        if (state & GDK_BUTTON1_MASK && pixmap != NULL)
                draw_brush(widget, x, y);
        }
        
        return TRUE;
}

// Selection marker to draw [pencil point].
static void 
select_pencil( GtkWidget *widget, gpointer data )
{
        actuall_brush = PENCIL;
        gtk_label_set_text(data, "pencil");
        gtk_widget_hide(text_entry);
		gtk_widget_hide(figure_filled);
}

// Selection marker to draw [line].
static void 
select_line( GtkWidget *widget, gpointer data )
{
        actuall_brush = LINE;
        gtk_label_set_text(data, "line");
        gtk_widget_hide(text_entry);
		gtk_widget_hide(figure_filled);
}

// Selection marker to draw [rectangle].
static void 
select_rectangle( GtkWidget *widget, gpointer data )
{
        actuall_brush = RECTANGLE;
        gtk_label_set_text(data, "rectangle");
        gtk_widget_hide(text_entry);
		gtk_widget_show(figure_filled);
}

// Selection marker to draw [circle].
static void 
select_ciricle( GtkWidget *widget, gpointer data )
{
        actuall_brush = CIRICLE;
        gtk_label_set_text(data, "circle");
        gtk_widget_hide(text_entry);
		gtk_widget_show(figure_filled);
}

// Selection eraser.
static void 
select_eraser( GtkWidget *widget, gpointer data )
{
        actuall_brush = ERASER;
        gtk_label_set_text(data, "eraser");
        gtk_widget_hide(text_entry);
		gtk_widget_hide(figure_filled);
}

// Selection text.
static void 
select_text( GtkWidget *widget, gpointer data )
{
        actuall_brush = TEXT;
        gtk_label_set_text(data, "text");
        gtk_widget_show(text_entry);
		gtk_widget_hide(figure_filled);
}

// Selection marker to clean the board.

static void
select_clean( GtkWidget *widget, gpointer data )
{
        actuall_brush = CLEAN;
        gtk_label_set_text(data, "cleaned");
        gtk_widget_hide(text_entry);
		gtk_widget_hide(figure_filled);
}

//Size selection [of the] drawing.
static void 
select_size( GtkWidget *widget, gpointer data )
{
        gchar *text =  gtk_combo_box_get_active_text(GTK_COMBO_BOX(widget)); 
        if(gtk_combo_box_get_active(GTK_COMBO_BOX(widget)) == 0)
        {
                brush_size = 8;
        }
        if(gtk_combo_box_get_active(GTK_COMBO_BOX(widget)) == 1)
        {
                brush_size = 14;
        }
        if(gtk_combo_box_get_active(GTK_COMBO_BOX(widget)) == 2)
        {
                brush_size = 20;
        }
        if(gtk_combo_box_get_active(GTK_COMBO_BOX(widget)) == 3)
        {
                brush_size = 26;
        }
        gtk_label_set_text(GTK_LABEL(data), text);
        g_free(text);
}

// Color selection.
static void 
select_color( GtkWidget *widget, gpointer data )
{
        gchar *text =  gtk_combo_box_get_active_text(GTK_COMBO_BOX(widget)); 
        color_gc = gdk_gc_new (widget->window);
        if(gtk_combo_box_get_active(GTK_COMBO_BOX(widget)) == 0)
        {
		  colors.red = 0; colors.green = 0; colors.blue = 0; // black
		  color = "black";
        }
        if(gtk_combo_box_get_active(GTK_COMBO_BOX(widget)) == 1)
        {
		  colors.red = 0xFFFF; colors.green = 0xFFFF; colors.blue = 0xFFFF; // white
		  color = "white";
        }
        if(gtk_combo_box_get_active(GTK_COMBO_BOX(widget)) == 2)
        {
		  colors.red = 0xFFFF; colors.green = 0; colors.blue = 0; // red
		  color = "red";
        }
        if(gtk_combo_box_get_active(GTK_COMBO_BOX(widget)) == 3)
        {
		  colors.red = 0; colors.green = 0; colors.blue = 0xFFFF; // blue
		  color = "blue";
        }
        if(gtk_combo_box_get_active(GTK_COMBO_BOX(widget)) == 4)
        {
		  colors.red = 0; colors.green = 0xFFFF; colors.blue = 0; // green
		  color = "green";
        }
        if(gtk_combo_box_get_active(GTK_COMBO_BOX(widget)) == 5)
        {
		  colors.red = 0xFFFF; colors.green = 0xFFFF; colors.blue = 0; // yellow
		  color = "yellow";
        }
        gdk_colormap_alloc_color (gdk_colormap_get_system(), &colors, FALSE, TRUE);
        gdk_gc_set_foreground (color_gc, &colors);
        
        gtk_label_set_text(GTK_LABEL(data), text);
        g_free(text);
}

// Fill the figure.
static void 
filled( GtkWidget *widget, gpointer data )
{
	if(figureFilled == TRUE)
		figureFilled = FALSE;
	else
		figureFilled = TRUE;
}

// Utworzenie tablicy do wspolnego rysowania
// Create an array to draw in common.
static void
create_board(PurpleBlistNode *node, gpointer user_data)
{
        GtkWidget *window;
        GtkWidget *fixed;
        GtkWidget *drawing_area;
        
        GtkWidget *pencil;
        GtkWidget *line;
        GtkWidget *ciricle;
        GtkWidget *rectangle;
        GtkWidget *color;
        GtkWidget *size;
        GtkWidget *eraser;
        GtkWidget *text;
        GtkWidget *clean;

        
        GtkWidget *label_1;
        GtkWidget *label_2;
        GtkWidget *label_3;
        GtkWidget *label_size;
        GtkWidget *label_color;

	buddy = (PurpleBuddy *)node;

        purple_debug_info(PLUGIN_ID, "INFO painting with: %s.\n", buddy->name);

        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
        gtk_window_set_default_size(GTK_WINDOW(window), 550, 550);
        char* title = g_strdup_printf("Paint Board: %s", buddy->name);
        gtk_window_set_title(GTK_WINDOW(window), title);
        
        fixed = gtk_fixed_new();
        gtk_container_add(GTK_CONTAINER(window), fixed);
        
        label_1 = gtk_label_new("SELECTED BRUSH:");
        gtk_fixed_put(GTK_FIXED(fixed), label_1, 10, 525);
        label_2 = gtk_label_new("SELECTED SIZE:");
        gtk_fixed_put(GTK_FIXED(fixed), label_2, 205, 525);
        label_3 = gtk_label_new("SELECTED COLOR:");
        gtk_fixed_put(GTK_FIXED(fixed), label_3, 370, 525);
        label_size = gtk_label_new("1x size");
        gtk_fixed_put(GTK_FIXED(fixed), label_size, 310, 525);
        label_color = gtk_label_new("black");
        gtk_fixed_put(GTK_FIXED(fixed), label_color, 490, 525);
        label_brush = gtk_label_new("pencil");
        gtk_fixed_put(GTK_FIXED(fixed), label_brush, 130, 525);
        
        // button-ski
        pencil = gtk_button_new_with_label("Pencil");
        gtk_fixed_put(GTK_FIXED(fixed), pencil, 10, 10);
        gtk_widget_set_size_request(pencil, 75, 25);
        g_signal_connect(G_OBJECT (pencil), "clicked", G_CALLBACK(select_pencil), label_brush);
        

        rectangle = gtk_button_new_with_label("Rectangle");
        gtk_fixed_put(GTK_FIXED(fixed), rectangle, 90, 10);
        gtk_widget_set_size_request(rectangle, 75, 25);
        g_signal_connect(G_OBJECT (rectangle), "clicked", G_CALLBACK(select_rectangle), label_brush);
        
        ciricle = gtk_button_new_with_label("Circle");
        gtk_fixed_put(GTK_FIXED(fixed), ciricle, 170, 10);
        gtk_widget_set_size_request(ciricle, 75, 25);
        g_signal_connect (G_OBJECT (ciricle), "clicked", G_CALLBACK(select_ciricle), label_brush);
        
        line = gtk_button_new_with_label("Line");
        gtk_fixed_put(GTK_FIXED(fixed), line, 90, 40);
        gtk_widget_set_size_request(line, 75, 25);
        g_signal_connect (G_OBJECT (line), "clicked", G_CALLBACK (select_line), label_brush);
        
        eraser = gtk_button_new_with_label("Eraser");
        gtk_fixed_put(GTK_FIXED(fixed), eraser, 10, 40);
        gtk_widget_set_size_request(eraser, 75, 25);
        g_signal_connect (G_OBJECT (eraser), "clicked", G_CALLBACK (select_eraser), label_brush);
        
        text = gtk_button_new_with_label("Text");
        gtk_fixed_put(GTK_FIXED(fixed), text, 170, 40);
        gtk_widget_set_size_request(text, 75, 25);
        g_signal_connect (G_OBJECT (text), "clicked", G_CALLBACK (select_text), label_brush);
        
        text_entry = gtk_entry_new();
        gtk_widget_set_size_request(text_entry, 95, 25);
        gtk_fixed_put(GTK_FIXED(fixed), text_entry, 250, 40);
		
	figure_filled = gtk_check_button_new_with_label("Figure filled");
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(figure_filled), TRUE);
	gtk_fixed_put(GTK_FIXED(fixed), figure_filled, 250, 12);
	gtk_signal_connect(GTK_OBJECT(figure_filled),"pressed", G_CALLBACK(filled), figureFilled);
        
        size = gtk_combo_box_new_text();
        gtk_combo_box_append_text(GTK_COMBO_BOX(size), "1x size");
        gtk_combo_box_append_text(GTK_COMBO_BOX(size), "2x size");
        gtk_combo_box_append_text(GTK_COMBO_BOX(size), "3x size");
        gtk_combo_box_append_text(GTK_COMBO_BOX(size), "4x size");
        gtk_widget_set_size_request(size, 90, 25);
        gtk_fixed_put(GTK_FIXED(fixed), size, 350, 10);
        gtk_combo_box_set_active(GTK_COMBO_BOX(size), 0);
        g_signal_connect(G_OBJECT(size), "changed", G_CALLBACK(select_size), label_size);
        
        color = gtk_combo_box_new_text();
        gtk_combo_box_append_text(GTK_COMBO_BOX(color), "black");
        gtk_combo_box_append_text(GTK_COMBO_BOX(color), "white");
        gtk_combo_box_append_text(GTK_COMBO_BOX(color), "red");
        gtk_combo_box_append_text(GTK_COMBO_BOX(color), "blue");
        gtk_combo_box_append_text(GTK_COMBO_BOX(color), "green");
        gtk_combo_box_append_text(GTK_COMBO_BOX(color), "yellow");
        gtk_widget_set_size_request(color, 90, 25);
        gtk_fixed_put(GTK_FIXED(fixed), color, 445, 10);
        gtk_combo_box_set_active(GTK_COMBO_BOX(color), 0);
        g_signal_connect(G_OBJECT(color), "changed", G_CALLBACK(select_color), label_color);
        
        // Obszar rysowania
        drawing_area = gtk_drawing_area_new ();
        gtk_drawing_area_size (GTK_DRAWING_AREA (drawing_area), 525, 445);
        gtk_fixed_put(GTK_FIXED(fixed), drawing_area, 10, 75);
        
        /* Signals used to handle backing pixmap */
        gtk_signal_connect(GTK_OBJECT (drawing_area), "expose_event", (GtkSignalFunc) expose_event, NULL);
        gtk_signal_connect(GTK_OBJECT (drawing_area), "configure_event", (GtkSignalFunc) configure_event, NULL);
        
        /* Event signals */
        gtk_signal_connect(GTK_OBJECT (drawing_area), "motion_notify_event", (GtkSignalFunc) motion_notify_event, NULL);
        gtk_signal_connect(GTK_OBJECT (drawing_area), "button_press_event", (GtkSignalFunc) button_press_event, NULL);
        
        gtk_widget_set_events(drawing_area, GDK_EXPOSURE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
        
        clean = gtk_button_new_with_label("Clean board");
        gtk_fixed_put(GTK_FIXED(fixed), clean, 350, 40);
        gtk_widget_set_size_request(clean, 185, 25);
        g_signal_connect (G_OBJECT (clean), "clicked", G_CALLBACK (select_clean), label_brush);
        
        gtk_widget_show_all(window);
        purple_debug_info(PLUGIN_ID, "INFO created window.\n");
        gtk_widget_hide(text_entry);
	gtk_widget_hide(figure_filled);
	
	char* msg = g_strdup_printf("Start Paintboard plugin");
        do_it_cb(msg);
        isWindowOpen = TRUE;
}

/**
 * Odbieranie i parsowanie wiadomości
 * Receiving and parsing of messages.
 */
static void
received_im_msg_cb(PurpleAccount *account, char *sender, char *buffer, PurpleConversation *conv, PurpleMessageFlags flags, void *data)
{
        purple_debug_info("signals test", "received-im-msg (%s, %s, %s, %s, %d)\n", purple_account_get_username(account), sender, buffer, PLUGIN_ID, flags);

        char* temp = buffer;
	PurpleBuddy *b; 
	const char* name = purple_conversation_get_name(conv);
	
	// Receive visual feedback [of the saved] drawing?
	if(strstr(temp,"Start Paintboard plugin") != 0 && isWindowOpen == FALSE) // odebranie inforamcji o checi rysowania
	{
		purple_debug_info(PLUGIN_ID, "PaintBoard starts.\n");
		if (account && (b = purple_find_buddy(account, name)) != NULL) { 
			create_board((PurpleBlistNode *)b, NULL);
		} else { 
			return;
		}
	}
	if(strstr(temp,":paintboard:") != 0) //test czy przesłano dane do rysowania (test whether the data to draw)
	{
	  int x1, y1, x2, y2 = 0;
	  char* r_color = "";
	  char* r_brush_type = "";
	  char* r_text = "";
	  char* r_filled = "";
	  char* cmd = "";
	  char* cmd1 = "";
	  char* cmd2 = "";
	  cmd = strtok(buffer, ":");
	  int i = 0;
	  while (cmd != NULL) {
		cmd = strtok(NULL, ":");
		if (i == 0) {
		       r_brush_type = cmd;
		       purple_debug_info(PLUGIN_ID, "SPLIT MESSAGE: brush type %s\n", r_brush_type);
		}
				if (i == 1) {
			r_color = cmd;
			purple_debug_info(PLUGIN_ID, "SPLIT MESSAGE: color %s\n", r_color);
		}
				if (i == 2) {
			cmd1 = cmd;
						purple_debug_info(PLUGIN_ID, "SPLIT MESSAGE: cord 1 %s\n", cmd1);
		}
		if (i == 3) {
			cmd2 = cmd;
						purple_debug_info(PLUGIN_ID, "SPLIT MESSAGE cord %s\n", cmd2);
		}
		if (i == 4) {
						r_text  = cmd;
						purple_debug_info(PLUGIN_ID, "SPLIT MESSAGE text %s\n", r_text);
		}
		if (i == 5) {
						r_filled  = cmd;
						purple_debug_info(PLUGIN_ID, "SPLIT MESSAGE filled %s\n", r_filled);
		}
		//purple_debug_info(PLUGIN_ID, "SPLIT MESSAGE: %s\n", cmd);
		i++;
		  }
		  int j = 0;
		  char* tmp = NULL;
		  tmp = strtok(cmd1, ",");
		  while (tmp != NULL) {
		if (j == 0) {
			x1 = atoi(tmp);
		}
		else if (j == 1) {
			y1 = atoi(tmp);
		}
		j++;
		purple_debug_info(PLUGIN_ID, "SPLIT MESSAGE 1: %s\n", tmp);
		tmp = strtok(NULL, ",");
		  }
		  j = 0;
		  tmp = strtok(cmd2, ",");
		  while (tmp != NULL) {
		if (j == 0) {
			x2 = atoi(tmp);
		}
		else if (j == 1) {
			y2 = atoi(tmp);
		}
		j++;
		purple_debug_info(PLUGIN_ID, "SPLIT MESSAGE 2: %s\n", tmp);
		tmp = strtok(NULL, ",");
		  }
		  purple_debug_info(PLUGIN_ID, "DRAW: %s, %d, %d, %d, %d, %s\n", r_color, x1, y1, x2, y2, r_text);
		  receive_draw_brush(my_widget, r_brush_type, r_color, x1, y1, x2, y2, r_text, r_filled);
		}
}

// Add the contextual menu.
static void
context_menu(PurpleBlistNode *node, GList **menu, gpointer plugin)
{
        PurpleMenuAction *action;
	PurpleBuddy *buddy;
	PurpleAccount *account;

        if (!PURPLE_BLIST_NODE_IS_BUDDY(node) && !PURPLE_BLIST_NODE_IS_CONTACT(node) &&
                !(purple_blist_node_get_flags(node) & PURPLE_BLIST_NODE_FLAG_NO_SAVE))
                return;
	buddy = (PurpleBuddy *)node;
        account = buddy->account;
	if (account == NULL) return;

        action = purple_menu_action_new(_("Draw with ..."), (PurpleCallback)create_board, NULL, NULL);
        (*menu) = g_list_prepend(*menu, action);
        purple_debug_info(PLUGIN_ID, "INFO context menu created, but not drawing yet.\n");
}

// Actions performed when you [enable] the plugin.
static gboolean
plugin_load(PurplePlugin *plugin)
{
        void *conv_handle     = purple_conversations_get_handle();

        purple_signal_connect(purple_blist_get_handle(), "blist-node-extended-menu", plugin, PURPLE_CALLBACK(context_menu), plugin);
        purple_signal_connect(conv_handle, "received-im-msg", plugin, PURPLE_CALLBACK(received_im_msg_cb), NULL);
        purple_debug_info(PLUGIN_ID, "INFO plugin loaded.\n");
        plugin = plugin;
        return TRUE;
}

// Actions performed when you disconnect [from] the plugin. 
static gboolean
plugin_unload(PurplePlugin *plugin)
{
        return TRUE;
}

//Information about the plugin.
static PurplePluginInfo info =
{
        PURPLE_PLUGIN_MAGIC,
        PURPLE_MAJOR_VERSION,
        PURPLE_MINOR_VERSION,
        PURPLE_PLUGIN_STANDARD,                                         /**< type           */
        PIDGIN_PLUGIN_TYPE,                                             /**< ui_requirement */
        0,                                                              /**< flags          */
        NULL,                                                           /**< dependencies   */
        PURPLE_PRIORITY_DEFAULT,                                        /**< priority       */
        "gtk-paintboard",                                               /**< id             */
        "Paint Board",                                                  /**< name           */
        "0.1 Beta",                                                     /**< version        */
        "Paint Board Plugin.",                                          /**  summary        */                                              
        "This plugin allows to draw on board with your friends.",      /**  description    */
        "Bartosz Ługowski <blugowski@gmail.com>\nWojciech Luks <wojciech.luks@gmail.com>", /**< author         */
        "http://code.google.com/p/pidgin-paintboard/",                  /**< homepage       */
        plugin_load,                                                    /**< load */
        plugin_unload,                                                  /**< unload */
        NULL,                                                           /**< destroy        */
        NULL,                                                           /**< ui_info        */
        NULL,                                                           /**< extra_info     */
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL
};

static void
init_plugin(PurplePlugin *plugin)
{
} 

PURPLE_INIT_PLUGIN(paint_board, init_plugin, info);
